import timeUtils from '@/utils/timeHandle.js';
import textFormatting from '@/utils/textFormatting.js';
export const changeStatusJS = {
  data() {
    return {
      ruleTypeDic: {
        GREY: '灰名单',
        BLACK: '黑名单',
        NORMAL: '正常',
        NOTSURE: '疑似异常',
        WHITE: '白名单'
      },
      notSureTypeDic: {
        GREY: '灰名单',
        BLACK: '黑名单',
        NOTSURE: '疑似异常',
        WHITE: '正常'
      },
      isRoot: Boolean(localStorage.getItem('full_rule_name'))
    };
  },

  methods: {
    // 日期修改
    dateChange(date) {
      if (date === null) {
        this.query.beginTime = '';
        this.query.endTime = '';
      } else {
        this.query.beginTime = `${timeUtils.timeHandle(
          date[0],
          'date'
        )} 00:00:00`;
        this.query.endTime = `${timeUtils.timeHandle(
          date[1],
          'date'
        )} 23:59:59`;
      }
    },
  }
};

export const watchRouterJs = {
  data() {
    return {
      isCache: false,
      cachePage: [
        'abnormalBehavior',
        'defaultRules',
        'exceptionUserManagement',
        'memberQuery'
      ]
    };
  },
  methods: {
    // 进入会员详情
    goToDetail(memberId) {
      this.$router.push({
        path: '/userExceptionFilter/userDetail',
        query: {
          memberId
        }
      });
    },

    // 规则格式化
    formatRule(data) {
      const rule = data.map(item => {
        item.smallMoney = textFormatting.numToFixed(item.smallMoney, 2);
        item.firstRuleGroupName = '';
        item.allRules = '';
        const rule = item.ruleGroupNameList;
        // 规则格式化
        rule.forEach((ruleItem, index) => {
          if (
            ruleItem.frequency == '' &&
                        ruleItem.operator == '' &&
                        ruleItem.periodOfTime == ''
          ) {
            if (index == 0) {
              item.firstRuleGroupName = ruleItem.ruleName;
            } else {
              ruleItem.ruleName = ruleItem.ruleName + ';';
            }
            item.allRules += ruleItem.ruleName;
          } else {
            let all = '';
            if (ruleItem.operator == '') {
              all = `${ruleItem.ruleName} ${ruleItem.periodOfTime}${
                ruleItem.frequency
              }`;
            } else {
              all = `${ruleItem.ruleName} ${ruleItem.periodOfTime}${
                ruleItem.frequency
              }${ruleItem.operator}${ruleItem.value}`;
            }

            if (index == 0) {
              item.firstRuleGroupName = all;
            }
            item.allRules += `${all} ; `;
          }
        });
        return item;
      });
      return rule;
    }
  },

  watch: {
    $route(to, from) {
      // 设置异常行为筛选/疑似异常会员名单/异常管理会员是否缓存[进入会员详情后回退缓存]
      if (from.path.indexOf('/userExceptionFilter/userDetail') !== -1 && this.cachePage.indexOf(to.name) !== -1) {
        this.isCache = true;
      } else {
        this.isCache = false;
      }
    }
  }
};
